--[[
Project: HunterRP
File: admin.lua
Author: Sebihunter
]]--
local tableNPCs = "npcs_"..game.GetMap()

function toggleLocalChat(ply, command, args)
    if ply:IsAdmin() then
		if tonumber(args[1]) == 1 then
			PrintMessageAll(HUD_PRINTTALK, "Local chat turned ON!")	
			ChatLimit = 1
		else
			PrintMessageAll(HUD_PRINTTALK, "Local chat turned OFF!")			
			ChatLimit = 0		
		end
    end 
end
concommand.Add("a_togglelocalchat", toggleLocalChat)

function makeCitizen(ply, command, args)
    if ply:IsAdmin() then
		for k, v in pairs(player.GetAll()) do
			if v:Nick() == tostring(args[1]) then
				v:SetTeam( 1 ) 
				v:Spawn()
			end
		end	
    end 
end
concommand.Add("a_makeciv", makeCitizen)

function makePolice(ply, command, args)
    if ply:IsAdmin() then
		for k, v in pairs(player.GetAll()) do
			if v:Nick() == tostring(args[1]) then
				v:SetTeam( 2 ) 
				v:Spawn()
			end
		end	
    end 
end
concommand.Add("a_makepol", makePolice)

function makeMedic(ply, command, args)
    if ply:IsAdmin() then
		for k, v in pairs(player.GetAll()) do
			if v:Nick() == tostring(args[1]) then
				v:SetTeam( 3 ) 
				v:Spawn()
			end
		end	
    end 
end
concommand.Add("a_makemed", makeMedic)

function makeMayor(ply, command, args)
    if ply:IsAdmin() then
		for k, v in pairs(player.GetAll()) do
			if v:Nick() == tostring(args[1]) then
				v:SetTeam( 4 ) 
				v:Spawn()
			end
		end	
    end 
end
concommand.Add("a_makemayor", makeMayor)

function toggleOwnable(ply, command, args)
    if ply:IsAdmin() then
		local trace = ply:GetEyeTrace()
		if ValidEntity(trace.Entity) and trace.Entity:GetPos():Distance(ply:GetPos()) < 400 then
			if trace.Entity:IsDoor() then	
				trace.Entity:SetNWInt("ownable", args[1])
			end
		end	
    end 
end
concommand.Add("a_ownable", toggleOwnable)

function togglePrice(ply, command, args)
    if ply:IsAdmin() then
		local trace = ply:GetEyeTrace()
		if ValidEntity(trace.Entity) and trace.Entity:GetPos():Distance(ply:GetPos()) < 400 then
			if trace.Entity:IsDoor() then	
				trace.Entity:SetNWInt("price", args[1])
			end
		end	
    end 
end
concommand.Add("a_price", togglePrice)

function toggleEconomy(ply, command, args)
    if ply:IsAdmin() then
		setCityEconomy( tonumber(args[1]) )
    end 
end
concommand.Add("a_economy", toggleEconomy)

function toggleName(ply, command, args)
    if ply:IsAdmin() then
		local trace = ply:GetEyeTrace()
		if ValidEntity(trace.Entity) and trace.Entity:GetPos():Distance(ply:GetPos()) < 400 then
			if trace.Entity:IsDoor() then	
				trace.Entity:SetNWString("name", table.concat(args, " "))
			end
		end	
    end 
end
concommand.Add("a_name", toggleName)

function toggleOwner(ply, command, args)
    if ply:IsAdmin() then
		local trace = ply:GetEyeTrace()
		if ValidEntity(trace.Entity) and trace.Entity:GetPos():Distance(ply:GetPos()) < 400 then
			if trace.Entity:IsDoor() then	
				trace.Entity:SetNWString("owner", table.concat(args, " "))
			end
		end	
    end 
end
concommand.Add("a_owner", toggleOwner)

function addNPC(ply, command, args)
    if ply:IsAdmin() then
		local pos = ply:GetPos()
		local angle = ply:GetAngles( )
		if tonumber(args[1]) == 1 then
			sql.Query( "INSERT INTO "..tableNPCs.." (`unique_id`, `npcid`, `x`, `y`, `z`, `rot`) VALUES ('"..gNPCNumber.."', '"..tonumber(args[1]).."', '"..(pos.x).."', '"..(pos.y).."', '"..(pos.z).."', '"..(angle.y).."')" )
			local npc = ents.Create("npc_eli")
			npc:SetPos(Vector(pos.x,pos.y,pos.z))
			npc:SetAngles(Angle(0, angle.y, 0))
			npc:SetModel( "models/barney.mdl" )
			npc:Spawn()
			npc:SetHullType( HULL_HUMAN )
			npc:SetHullSizeNormal( )
			npc:SetNPCState( NPC_STATE_SCRIPT )
			npc:SetSolid(  SOLID_BBOX )
			npc:CapabilitiesAdd( CAP_ANIMATEDFACE | CAP_TURN_HEAD )
			npc:SetUseType( SIMPLE_USE )
			npc:DropToFloor()
				
			npc:SetNWInt("unique_id", gNPCNumber)
			npc:SetNWInt("npcid", tonumber(args[1]))
			
			gNPCNumber = gNPCNumber+1
		elseif tonumber(args[1]) == 2 then
			sql.Query( "INSERT INTO "..tableNPCs.." (`unique_id`, `npcid`, `x`, `y`, `z`, `rot`) VALUES ('"..gNPCNumber.."', '"..tonumber(args[1]).."', '"..(pos.x).."', '"..(pos.y).."', '"..(pos.z).."', '"..(angle.y).."')" )
			local npc = ents.Create("npc_eli")
			npc:SetPos(Vector(pos.x,pos.y,pos.z))
			npc:SetAngles(Angle(0, angle.y, 0))
			npc:SetModel( "models/mosman.mdl" )
			npc:Spawn()
			npc:SetHullType( HULL_HUMAN )
			npc:SetHullSizeNormal( )
			npc:SetNPCState( NPC_STATE_SCRIPT )
			npc:SetSolid(  SOLID_BBOX )
			npc:CapabilitiesAdd( CAP_ANIMATEDFACE | CAP_TURN_HEAD )
			npc:SetUseType( SIMPLE_USE )
			npc:DropToFloor()
				
			npc:SetNWInt("unique_id", gNPCNumber)
			npc:SetNWInt("npcid", tonumber(args[1]))
			
			gNPCNumber = gNPCNumber+1	
		elseif tonumber(args[1]) == 3 then
			sql.Query( "INSERT INTO "..tableNPCs.." (`unique_id`, `npcid`, `x`, `y`, `z`, `rot`) VALUES ('"..gNPCNumber.."', '"..tonumber(args[1]).."', '"..(pos.x).."', '"..(pos.y).."', '"..(pos.z).."', '"..(angle.y).."')" )
			local npc = ents.Create("npc_eli")
			npc:SetPos(Vector(pos.x,pos.y,pos.z))
			npc:SetAngles(Angle(0, angle.y, 0))
			npc:SetModel( "models/alyx.mdl" )
			npc:Spawn()
			npc:SetHullType( HULL_HUMAN )
			npc:SetHullSizeNormal( )
			npc:SetNPCState( NPC_STATE_SCRIPT )
			npc:SetSolid(  SOLID_BBOX )
			npc:CapabilitiesAdd( CAP_ANIMATEDFACE | CAP_TURN_HEAD )
			npc:SetUseType( SIMPLE_USE )
			npc:DropToFloor()
				
			npc:SetNWInt("unique_id", gNPCNumber)
			npc:SetNWInt("npcid", tonumber(args[1]))
			
			gNPCNumber = gNPCNumber+1			
		else
			ply:PrintMessage(HUD_PRINTTALK, "Wrong NPC type!")	
		end
    end 
end
concommand.Add("a_npc", addNPC)